#include "ValidatorTest.h"

#include <QtTest/QtTest>

#include "QDomDocument"

#include "TestSuite.h"
#include "XmlParser.h"
#include "BBClass.h"
#include "Validator.h"

void ValidatorTest::check()
{
	QString xmlMappingFileName( "TestValidator.xml" );
	QFile xmlMappingFile( xmlMappingFileName );
	QString className( "TestClass" );
	QString attributeName( "testAttribute" );
	QString attributeType( "int" );
	QString classExtension( "SuperClass" );
	
	if( !xmlMappingFile.open( QIODevice::WriteOnly) )
	{
		QVERIFY( false );
		return;
	}

	QString xmlCode = QString( "%1%2%3%4%5" )
		.arg("<blackbear-mapping>\n")
		.arg( "<class name=\"" + className + "\"" + ">\n" )
		.arg( "<attribute name=\"" + attributeName + "\" type=\"" + attributeType + "\"/>\n" )
		.arg( "</class>\n" )
		.arg("</blackbear-mapping>\n");

	QTextStream ts( &xmlMappingFile );
	ts << xmlCode;
	xmlMappingFile.close();

	QFile xmlMappingFileLoad( xmlMappingFileName );

	//Check if file opened
	if( !xmlMappingFileLoad.open( QFile::ReadOnly ) )
	{
		QVERIFY( false );
		xmlMappingFileLoad.close();
		xmlMappingFileLoad.remove();
		return;
	}

	QDomDocument doc;
	doc.setContent( &xmlMappingFileLoad );

	Validator* validator = Validator::instance();
	QString errorMessage( validator->check( doc ) );

	QCOMPARE( errorMessage, QString( "" ) );

	xmlMappingFileLoad.remove();
}

REGISTER_TEST_CLASS( ValidatorTest );